﻿using System;
using System.Linq;
using MeetSomeNearbyStrangerWebApi.Models;
using MeetSomeNearbyStranger.DataModels;

namespace MeetSomeNearbyStrangerWebApi.Validators
{
    public class UserValidator
    {
        private const string ValidNicknameChars =
            "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_1234567890 -";
        private const int MinNicknameNicknameChars = 4;
        private const int MaxNicknameNicknameChars = 30;

        public static void ValidateNickname(string nickname)
        {
            if (nickname == null || nickname.Length < MinNicknameNicknameChars || nickname.Length > MaxNicknameNicknameChars)
            {
                throw new ArgumentException(string.Format(
                    "Nickname should be between {0} and {1} symbols long",
                    MinNicknameNicknameChars,
                    MaxNicknameNicknameChars), "nickname");
            }
            else if (nickname.Any(ch => !ValidNicknameChars.Contains(ch)))
            {
                throw new ArgumentException("Nickname contains invalid characters", "nickname");
            }
        }

        public static void ValidateAge(int age)
        {
            if (age < 0 || age > 120)
            {
                throw new ArgumentException("Age is invalid.");
            }
            else if (age < 18)
            {
                throw new ArgumentException("You must be at least 18 to access the content of the app.");
            }
        }

        public static void ValidateGender(string gender)
        {
            if (gender != "Male" && gender != "Female")
            {
                throw new ArgumentException("Gender is not properly specified.");
            }
        }

        public static void ValidateNewUserData(UserRegisterModel user)
        {
            ValidateNickname(user.Nickname);
            ValidateAge(user.Age);
            ValidateGender(user.Gender);
        }

        public static void ValidateNewUserData(User user)
        {
            ValidateNickname(user.Nickname);
            ValidateAge(user.Age);
            ValidateGender(user.Gender);
        }

        public static void ValidateNewUserData(ProfileModel user)
        {
            ValidateNickname(user.Nickname);
            ValidateAge(user.Age);
            ValidateGender(user.Gender);
        }
    }
}